package com.nlp.trie.experiment3;

import java.util.Map;
import java.util.TreeMap;

import com.google.gson.Gson;

/**
 * 词条
 * @author ygsong.abcft
 *
 */
public class Term implements Comparable<Term>{

	/**单词*/
	private String word;
	
	/**包含该单词的文档个数*/
	private int ndoc;
	/**单词出现的次数*/
	private int freq;
	/**
	 * 单词对应文档的次数
	 * <文档id, 单词次数>
	 * */
	private Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
	
	public Term() {
	}
	@Override
	public int hashCode() {
		return word.hashCode();
	}
	@Override
	public String toString() {
		Gson gson = new Gson();
		return gson.toJson(this);
	}
	
	@Override
	public int compareTo(Term o) {
		return word.compareTo(o.word);
	}
	
	public Term add(int docId) {
		freq++;
		ndoc = map.containsKey(docId) ? ndoc:ndoc+1;
		map.put(docId, map.getOrDefault(docId, 0)+1);
		return this;
	}
	
	public String getWord() {
		return word;
	}
	public void setWord(String word) {
		this.word = word;
	}
	public int getNdoc() {
		return ndoc;
	}
	public void setNdoc(int ndoc) {
		this.ndoc = ndoc;
	}
	
	public void setFreq(int freq) {
		this.freq = freq;
	}
	public int getFreq() {
		return freq;
	}
	
	public Map<Integer, Integer> getMap() {
		return map;
	}
	public void setMap(Map<Integer, Integer> map) {
		this.map = map;
	}
	

}
